<template>
  <div class="activity">
    <div>
      <div class="tabs">
        <div
          class="tab-item"
          v-for="(item, index) of tabs"
          :key="index"
          @click="tabItemClick(index)"
          :class="{'active': activeIndex === index}"
        >{{item}}</div>
        <div class="tab-angle" :style="{left: anLeft +'rem'}">
          <img src="@/assets/images/icon_sjb.png" alt />
        </div>
      </div>
      <Scroll
        ref="activityScroll"
        class="scroll-container"
        :data="activityList"
        :pullUp="true"
        :listenScroll="true"
        :probeType="3"
        @scrollToEnd="handleScrollToEnd"
      >
        <div class="content" v-if="activityList.length">
          <div v-for="(item, index) of activityList" :key="index" style="margin-bottom: 0.41rem">
            <panel-item-act :panelInfo="item" @click.native="panelActClick(item)" />
          </div>
          <van-loading size="24px" v-if="isMore">加载中...</van-loading>
        </div>
        <no-result v-else :src="require('@/assets/images/1-zwnr.png')" msg="暂无活动" />
      </Scroll>
    </div>
    <transition name="slide">
      <router-view></router-view>
    </transition>
  </div>
</template>
<script>
import PanelItemAct from '@/components/panel-item-act'
import Scroll from '@/components/scroll/scroll'
import { getActivityList } from '@/api/home'
// import setView from './mixins/setView'
import NoResult from '@/components/no-result/no-result'
import wx from 'weixin-js-sdk'
const ERR_OK = 200
export default {
  components: {
    PanelItemAct,
    Scroll,
    NoResult
  },
  // mixins: [setView],
  data () {
    return {
      tabs: ['近期活动', '活动回顾'],
      activeIndex: 0,
      anLeft: 0.8,
      activityList: [],
      status: 1,
      page: 1,
      isMore: true
    }
  },
  created () {
    this._getActivityList(this.status, this.page)
  },
  beforeRouteUpdate (to, from, next) {
    // ...
    this.$notify.clear()
    next()
  },
  methods: {
    panelActClick (item) {
      if (item.link) {
        wx.miniProgram.getEnv(ress => {
          if (ress.miniprogram) {
            wx.miniProgram.navigateTo({
              url: `/pages/index/index?return_url=${encodeURIComponent(
                item.link
              )}`
            })
          }
        })
      } else {
        this.$router.push({ name: 'ActivityDetail', params: { id: item.id } })
      }
    },
    handleScrollToEnd () {
      this.page += 1
      if (this.isMore) {
        this._getActivityList(this.status, this.page).then(res => {
          if (res.data.data.length) {
            this.$notify({
              type: 'success',
              message: `已为您更新${res.data.data.length}条数据`,
              className: 'my-cls',
              duration: 1000
            })
          }
        })
      } else {
        this.$notify({
          type: 'warning',
          message: '没有更多内容了~',
          className: 'my-cls',
          duration: 1000
        })
      }
    },
    tabItemClick (index) {
      this.activeIndex = index
      this.$refs.activityScroll.scrollTo(0, 0)
      this.isMore = true
      this.activityList = []
      this.page = 1
      switch (index) {
        case 0:
          this.anLeft = 0.8
          this.status = 1
          this._getActivityList(this.status, this.page)
          break
        case 1:
          this.status = 0
          this._getActivityList(this.status, this.page)
          this.anLeft = 2.7
          break
      }
    },
    hasMore () {
      if (this.activityList.length === this.total) {
        this.isMore = false
      } else {
        this.isMore = true
      }
    },
    _getActivityList (status, page) {
      return getActivityList(status, page)
        .then(res => {
          if (res.code === ERR_OK) {
            this.activityList = this.activityList.concat([...res.data.data])
            this.total = res.data.total
            this.hasMore()
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/mixin.scss";

.activity {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  z-index: 2;
  touch-action: none;
  display: flex;
  flex-direction: column;
  .tabs {
    width: 100%;
    height: 1rem;
    background: rgba(255, 255, 255, 1);
    padding: 0 0.36rem;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-start;
    position: relative;
    .tab-item {
      font-size: 0.28rem;
      line-height: 0.28rem;
      font-family: PingFang SC;
      font-weight: 300;
      color: rgba(51, 51, 51, 1);
      text-align: center;
      margin-right: 0.71rem;
      padding: 0.36rem 0;
      &.active {
        font-weight: bold;
      }
    }
    .tab-angle {
      position: absolute;
      bottom: 0;
      width: 0.12rem;
      height: 0.06rem;
      transition: all 0.15s;
      img {
        width: 100%;
        vertical-align: top;
      }
    }
  }
  .scroll-container {
    position: absolute;
    left: 0;
    top: 1rem;
    right: 0;
    bottom: 0;
    overflow: hidden;
    .content {
      width: 100%;
      padding: 0.43rem 0.37rem;
      box-sizing: border-box;
      .panel-item-act {
        margin-bottom: 0.41rem;
      }
    }
  }
}
.van-loading {
  text-align: center;
  padding: 0.3rem 0;
}
@include slideEnter();
</style>
